<template>
  <div>
    <el-form ref="myform" :model="formItem" :rules="rules" label-width="100px" size="mini">
      <el-row :gutter="40">
        <el-col :span="24">
          <el-form-item label="标题" prop="title">
            <el-input v-model="formItem.title" placeholder="请填写工单标题" />
          </el-form-item>
        </el-col>
        <!--  <el-col :span="12">
          <el-form-item label="综合管理部" prop="comprehensive">
            <el-radio-group v-model="formItem.comprehensive">
              <el-radio :label="true">是</el-radio>
              <el-radio :label="false">否</el-radio>
            </el-radio-group>
          </el-form-item>
        </el-col> -->
        <el-col :span="24">
          <el-form-item label="备注" prop="remark">
            <el-input v-model="formItem.remark" type="textarea" placeholder="请填写备注" />
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="资产明细">
            <el-button size="mini" type="primary" icon="el-icon-search" style="margin-bottom: 15px;" @click="assetDialogFormVisible = true">选择资产</el-button>
            <PageableTable
              :columns="insideColumns"
              :loading="false"
              :table-height="240"
              :data="formItem.details"
              :disable-pageable="true"
              border
              class="fixed-table-table"
            >
              <el-table-column
                label="序号"
                type="index"
                width="50"
                header-align="center"
                align="center"
              />
              <el-table-column v-for="column in insideColumns" :key="column.key" header-align="center" align="center" :label="column.title" :prop="column.key" :sortable="column.sortable">
                <template slot-scope="scope">
                  <column-render :render="column.render" :row="scope.row" :column="column" />
                </template>
              </el-table-column>
              <el-table-column label="更换原因" header-align="center" align="center" width="200px">
                <template slot-scope="scope">
                  <el-input v-model="scope.row.reason" type="text" size="mini" maxlength="20" />
                </template>
              </el-table-column>
              <el-table-column label="操作" header-align="center" align="center" width="80" class-name="small-padding fixed-width">
                <template slot-scope="{row}">
                  <el-button type="text" size="mini" icon="el-icon-delete" title="删除" @click="remove(row)">移除</el-button>
                </template>
              </el-table-column>
            </PageableTable>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="">
            <el-button size="mini" @click="btnCancel">取 消</el-button>
            <el-button type="primary" @click="commit">提交</el-button>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <SelectDialog title="选择资产" :append-to-body="true" :show="assetDialogFormVisible" @on-ok="selectOk" @on-cancel="assetDialogFormVisible = false">
      <assets
        v-if="assetDialogFormVisible"
        ref="assets"
        belongs="kj"
        :status-search-show="false"
        status-code="used"
        :for-selection="true"
        categories="1"
        :locked="false"
      />
    </SelectDialog>
  </div>
</template>
<script>
import { create, detail } from '@neu/dcam-ui/api/genghuanKJ'
import PageableTable from '@neu/biz-core/components/PageableTable'
import SelectDialog from '@neu/biz-core/components/SelectDialog'
import assets from '@neu/dcam-ui/views/fixedAsset/tables'
const formItem = {
  title: '', // 工单标题
  remark: '',
  workOrderNum: '',
  taskId: '',
  comprehensive: true,
  details: []
}
export default {
  components: {
    PageableTable,
    SelectDialog,
    assets
  },
  props: {
    taskId: {
      required: false,
      type: String,
      default: 'NULL'
    },
    workOrderNum: {
      required: false,
      type: String,
      default: ''
    },
    orderStatus: {
      required: false,
      type: String,
      default: 'NULL'
    }
  },
  data() {
    return {
      /** 是否显示资产选择对话框 */
      assetDialogFormVisible: false,
      // 编辑的表单内容
      formItem: {
        ...formItem
      },
      // 表单验证规则
      rules: {
        title: [{
          required: true,
          message: '标题不能为空',
          trigger: 'blur'
        }]
      },
      insideColumns: [
        { title: '资产名称', key: 'name' },
        { title: '资产编号', key: 'code' },
        { title: '资产分类', key: 'type',
          render: (h, params) => {
            let txt = params.row.smallTypeName
            if (params.row.middleTypeName) {
              txt = params.row.middleTypeName + ' / ' + txt
            }
            if (params.row.bigTypeName) {
              txt = params.row.bigTypeName + ' / ' + txt
            }
            return h('span', txt)
          }
        },
        { title: '规格型号', key: 'specifications' },
        { title: '序列号', key: 'serialnumber' },
        { title: '使用人', key: 'managementUserName' }
      ]
    }
  },
  created() {
    this.fetch()
  },
  methods: {
    // 获取借用工单信息
    fetch() {
      if (this.workOrderNum !== 'NULL') {
        detail(this.workOrderNum, this.taskId).then(response => {
          this.formItem = {
            ...response.data
          }
        })
      } else {
        formItem.details = []
        this.formItem = {
          ...formItem
        }
      }
    },
    /** 移除一条数据 */
    remove(row) {
      const i = this.formItem.details.lastIndexOf(row)
      this.formItem.details.splice(i, 1)
    },
    commit() {
      this.$refs.myform.validate((valid) => {
        if (valid) {
          // 更换资产不能为空
          if (this.formItem.details.length === 0) {
            return this.$message({
              showClose: true,
              message: '资产明细为空，请填写资产明细后在提交!',
              type: 'error'
            })
          }

          const temp = this.formItem.details.filter(v => {
            return (v.reason === '' || v.reason === null)
          })
          if (temp.length > 0) {
            return this.$message({
              showClose: true,
              message: '更换原因不能为空,请填写!',
              type: 'error'
            })
          }

          const data = { ...this.formItem }
          if (this.orderStatus === 'revoked') {
            data.workOrderNum = null
            data.taskId = null
          } else {
            data.workOrderNum = this.workOrderNum
            data.taskId = this.taskId
          }
          // ge.x 2022-04-08 增加锁屏功能
          const loading = this.$loading({
            lock: true,
            text: 'Loading',
            spinner: 'el-icon-loading',
            background: 'rgba(0, 0, 0, 0.7)'
          })
          create(data).then(res => {
            if (res.status === 'success') {
              loading.close()
              this.$emit('cancelNewOrderForm', 'refresh')
            }
          }).catch(() => {
            loading.close()
            this.$emit('cancelNewOrderForm', 'refresh')
          })
        }
      })
    },
    selectOk() {
      const assets = this.$refs.assets.selections
      const assetCodes = this.formItem.details.map(a => a.code)
      const difference = assets.filter(t => {
        return !assetCodes.includes(t.code)
      })
      difference.forEach(element => {
        this.formItem.details.push(element)
      })
      this.assetDialogFormVisible = false
    },
    btnCancel() {
      this.$emit('cancelNewOrderForm', 'refresh')
    }
  }
}
</script>
